
let audio = cc.audioEngine

class AudioSystem
{
    constructor() 
    {
        this.init()
    }

    static inst = null

    static share() 
    {
        this.inst || (this.inst = new AudioSystem())

        return this.inst
    }

    init() 
    {
        const p = require("ConstBase")

        let ss = require("StorageSystem").share()

        let volume = ss.get(p.music_volume)
        if (volume === undefined || volume === null)
        {
            this.mMusicVolume = 1
        }
        else
        {
            this.mMusicVolume = volume
        }

        volume = ss.get(p.sound_volume)
        if (volume === undefined || volume === null)
        {
            this.mSoundVolume = 1
        }
        else
        {
            this.mSoundVolume = volume
        }

        this.mMusics = []
    }

    preloadAudio(audioClip) 
    {
        let url = 'audio/' + fileList[audioClip]

        ogre.loadResource(url, cc.AudioClip, 3)
    }

    pause()
    {   
        audio.pauseAll()
    }

    resume()
    {
        audio.resumeAll()
    }
    
    play(id, loop, music, callback) 
    {
        const Audios = require('Audios')
        if (!Audios)
        {
            return
        }

        let url = 'audio/'

        let d = Audios[id]

        if (d) 
        {
            if (typeof d === 'string') 
            {
                url += d
            }
            else
            {
                url += d[Math.randomInt(0, d.length-1)]
            }
        }
        else
        {
            url += id
        }

        ogre.loadResource(url, cc.AudioClip, 3,
            (clip) =>
            {
                if (!clip)
                {
                    return
                }

                let aid = audio.play(clip, loop ? true : false, music ? this.mMusicVolume : this.mSoundVolume)
                if (aid >= 0)
                {
                    if (callback && !callback(aid, false))
                    {
                        audio.stop(aid)
                    }
                    else
                    {
                        this.mMusics.push(aid)

                        audio.setFinishCallback(aid, 
                            () =>{

                                let i = this.mMusics.indexOf(aid)
                                if (i >= 0)
                                {
                                    this.mMusics.splice(i, 1)
                                }
                                callback && callback(aid, true)
                            }
                        )
                    }
                }
            }
        )
    }

    stop(aid) 
    {
        if (aid  || aid === 0)
        {
            audio.stop(aid)  

            let i = this.mMusics.indexOf(aid)
            if (i >= 0)
            {
                this.mMusics.splice(i, 1)
            }
        }
        else
        {
            audio.stopAll()

            let n = this.mMusics.length

            if (n > 0)
            {
                this.mMusics.splice(0, n)
            }
        }
    }

    clear() 
    {
        audio.uncacheAll()
    }

    volumeByMusic() 
    {
        return this.mMusicVolume
    }

    volumeBySound() 
    {
        return this.mSoundVolume
    }

    setMusicVolume(v) 
    {
        if (v > 1)
        {
            v = 1
        }
        else if (v < 0)
        {
            v = 0
        }

        if (v != this.mMusicVolume)
        {
            this.mMusicVolume = v
            
            for(let o of this.mMusics)
            {
                audio.setVolume(o, v)
            }

            let ss = require("StorageSystem").share()
            ss.set(require("ConstBase").music_volume, v, true)
        }
    }

    setSoundVolume(v) 
    {
        if (v > 1)
        {
            v = 1
        }
        else if (v < 0)
        {
            v = 0
        }

        if (v != this.mSoundVolume)
        {
            this.mSoundVolume = v

            let ss = require("StorageSystem").share()
            ss.set(require("ConstBase").sound_volume, v, true)
        }
    }
}

export default AudioSystem
